<?php
/*
 * @分销商城
 * @功能说明：客户管理中心
 * @更新说明：暂无更新
 * @文件名 IndexController.class.php
 * @编码 UTF-8
 * @创建时间 2015-12-25 下午1:46:53
 * @创建人 XING
 */
namespace dg\Controller;

use Think\Controller;

header("Content-type: text/html; charset=utf-8");

class IndexController extends ApibaseController
{
    /*
     * 用户列表
     * @date: 2015-6-2 上午11:04:55
     * @editor: XING
     */
    public function index() {
        /*参数初始化*/
        $page = I('get.page') ?: 1;
        $perpage = 10;
        $url = '';
        $list = M('CustEntity');
        $url = $output = $outtype = array();
        if (I('get.cust_name')) {
            $list = $list->where('cust_realname LIKE "%' . I('get.cust_name') . '%"');
            $url['cust_name'] = I('get.cust_name');
        }
        if (I('get.cust_mobile')) {
            $list = $list->where(array('cust_mobile' => I('get.cust_mobile')));
            $url['cust_mobile'] = I('get.cust_mobile');
        }
        if (I('get.cust_id')) {
            $list = $list->where(array('cust_id' => I('get.cust_id')));
            $url['cust_id'] = I('get.cust_id');
        }
        if (I('get.level')) {
            $list = $list->where(array('salelevel' => I('get.level')));
            $url['level'] = I('get.level');
        }

        $custObject = clone $list;
        $getData = $list->order(array('add_at' => 'desc'))->page($page, $perpage)->select();
        $this->assign('page', setPage($custObject->count(), $perpage, $page, '?' . http_build_query($url)));
        $this->assign('getData', $getData);

        $this->display('index/index');
    }


    /**
     * 用户信息修改
     * @author GaoChao
     */
    public function userEdit() {
        if (I('post.dosubmit')) {
            $ups = array(
                'cust_realname' => I('post.cust_realname'),
                'cust_mobile'   => I('post.cust_mobile'),
                'update_at'     => time(),
                'nickname'      => I('post.nickname'),
                'cust_img'      => I('post.cust_img'),
                'click_url'     => I('post.click_url')
            );
            $affect = M('CustEntity')->where(array(
                'cust_id' => I('post.cust_id')
            ))->setField($ups);
            if ($affect) {
                putlog('用户信息修改成功 ID`' . I('post.cust_id') . '`', 'IndexController', 'userEdit');
                $this->showtips('用户信息修改成功');
            } else {
                $this->showtips('操作失败');
            }
        }
        if (I('get.cust_id')) {
            $user = M('CustEntity')->where(array(
                'cust_id' => I('get.cust_id')
            ))->find();

            $this->assign('user', $user);
        }

        $this->display('index/user_edit');
    }

    /*
	 * 用户等级
	 * @date: 2016-5-6
	 * @editor: yanyan
	 */
    public function level() {
        $getData = $this->get_child(0);
        $this->assign('getData', $getData);
        $this->display('index/level');
    }

    //获取用户子分类(递归)
    public function get_child($id) {
        $arr = array();
        $rows = M('CustEntity')
            ->where('salelevel != 0 AND parent_id = ' . $id)->field('cust_id,parent_id,cust_realname,salelevel')->select();
        if ($rows) {
            foreach ($rows as $k => $v) {
                $rows[$k]['list'] = $this->get_child($v['cust_id']);
                $arr[] = $rows;//组合数组
            }

            return $rows;
        }
    }

    /*
     * 开通顶级用户
     * @date: 2015-6-2 下午12:08:52
     * @editor: XING
     */
    public function putTop() {
        if (I('post.dosubmit')) {
            $userMsg = M('CustEntity')
                ->where(array('cust_id' => I('post.cust_id')))->find();
            if ($userMsg['salelevel'] > 0) {
                $this->showtips('仅限非会员开通顶级身份');
                exit;
            } else {
                if (I('post.cust_id') && in_array(I('post.level'), array('1', '2', '3'))) {
                    $data = array(
                        'lastest_id' => I('post.cust_id'),
                        'salelevel'  => I('post.level'),
                        'is_top'     => '1',
                        'promit_at'  => time(),
                        'update_at'  => time()
                    );
                    if (I('post.level') == '3') {
                        $data['zj_type'] = '2';
                    }
                    M('CustEntity')
                        ->where(array('cust_id' => I('post.cust_id')))->setField($data);
                    putlog('用户顶级开通 `' . $userMsg['cust_id'] . '`', 'IndexController', 'putTop');
                    $this->showtips('开通成功');
                } else {
                    $this->showtips('数据非法，操作失败');
                }
            }
        } elseif (I('get.cust_id')) {
            $checkStatus = M('CustEntity')
                ->getFieldByCustId('salelevel', I('get.cust_id'));
            if ($checkStatus > 0) {
                $this->showtips('仅限非会员开通顶级身份');
                exit;
            } else {
                $this->assign('cust_id', I('get.cust_id'));
                $this->display('index/top');
            }
            $this->display('index/top');
        }
    }

    /*
     * 冻结&解封用户
     * @date: 2015-6-2 下午2:34:17
     * @editor: XING
     */
    public function decate() {
        if (I('get.types')) {
            M('CustEntity')->where(array('cust_id' => I('get.custid')))->setField(array(
                'is_check' => '1'
            ));
            $this->showtips('成功解封该用户');
        }
        M('CustEntity')->where(array('cust_id' => I('get.custid')))->setField(array(
            'is_check' => '0'
        ));
        $this->showtips('成功冻结该用户');
    }

    /*
     * 定时统计功能
     * @date: 2015-6-3 下午4:22:19
     * @editor: XING
     */
    public function getCountCj() {
        $tj_life_regist = M('CustEntity')->where('add_at > ' . strtotime(date('Y-m-d')) . ' AND add_at < ' . time())->count();
        $tj_life_port = M('ReportEntity')->where('add_at > ' . strtotime(date('Y-m-d')) . ' AND add_at < ' . time())->count();
        session(array('expire' => 360000));
        session('tj_life', time());
        session('tj_life_regist', $tj_life_regist);
        session('tj_life_port', $tj_life_port);
    }

}